﻿Imports FairShop.Helpers
Imports System.Data.OleDb

Public Class BarcodeDal

    Private Const SELECT_NIEUWE_BARCODE As String = "SELECT MAX(Barcode) FROM Artikels WHERE Barcode LIKE '29%'"
    Private Const SELECT_DUBBELE_BARCODES As String = "SELECT Barcode FROM Artikels " & _
                                                      "WHERE Barcode <> '' AND Verkoopbaar <> 0 " & _
                                                      "GROUP BY Barcode HAVING Count(*)>1"

    Public Shared Function GetNieuweBarcode() As String
        Dim conn As New OleDbConnection(My.Settings.fairshopConnectionString)
        Try
            conn.Open()
            Dim cm As New OleDbCommand(SELECT_NIEUWE_BARCODE, conn)
            Dim laatstGebruikteBarcode As String = DirectCast(cm.ExecuteScalar(), String)
            Return BarcodeHelper.GetNextBarcode(laatstGebruikteBarcode)
        Catch ex As Exception
            log.WriteError("BarcodeDal.GetNieuweBarcode", ex)
            Return "Fout opgetreden"
        Finally
            conn.Close()
        End Try
    End Function

    Public Shared Function GetDubbeleBarcodes() As List(Of String)
        Dim lijst As New List(Of String)
        Dim conn As New OleDbConnection(My.Settings.fairshopConnectionString)
        Try
            conn.Open()
            Dim cm As New OleDbCommand(SELECT_DUBBELE_BARCODES, conn)
            Dim dr As OleDbDataReader = cm.ExecuteReader()
            While dr.Read()
                lijst.Add(dr(0).ToString())
            End While
        Catch ex As Exception
            log.WriteError("BarcodeDal.GetDubbeleBarcodes", ex)
        Finally
            conn.Close()
        End Try
        Return lijst
    End Function

End Class
